package com.lagou.controller;

import com.lagou.config.LoadBalanse;
import com.lagou.config.RandomLoadBalance;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class TestController {

    private RestTemplate restTemplate = new RestTemplate();

    private LoadBalanse loadBalanse = new RandomLoadBalance();

    @RequestMapping("/getOrder")
    public Object getProduct(@RequestBody Map entity) {
        String host = loadBalanse.choseServiceHost();
        long startTime = System.currentTimeMillis();
        Map res = restTemplate.postForObject("http://" + host + "/product/getProduct", entity, Map.class);
        long endTime = System.currentTimeMillis();
        long costTime = endTime - startTime;
        System.out.println("rpc耗时====" + host + "--" + costTime + "ms");
        loadBalanse.setTimeMap(host, costTime);
        res.put("host", host);
        return res;
    }

    @RequestMapping("/test")
    public Map test() {
        return null;
    }
}
